package com.ruoyi.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.entity.DTO.LegalAidLawyerDTO;
import com.ruoyi.entity.LegalAidLawyer;
import com.ruoyi.entity.vo.LegalAidLawyerVO;
import com.ruoyi.mapper.LegalAidLawyerWxMapper;
import com.ruoyi.mapper.SysUserWxMapper;
import com.ruoyi.properties.WeChatProperties;
import com.ruoyi.service.ILegalAidLawyerWxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 律师信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-09
 */
@Service
@Slf4j
@SuppressWarnings("all")
public class LegalAidLawyerWxServiceImpl implements ILegalAidLawyerWxService
{
    //微信服务接口地址
    private static final String WX_LOGIN ="https://api.weixin.qq.com/sns/jscode2session";
    @Autowired
    private LegalAidLawyerWxMapper legalAidLawyerWxMapper;
    @Autowired
    private SysUserWxMapper sysUserWxMapper;
    @Autowired
    private WeChatProperties weChatProperties;

    /**
     * 查询律师信息列表
     *
     * @param legalAidLawyerDTO 律师信息
     * @return 律师信息
     */
    @Override
    public List<LegalAidLawyerVO> selectLegalAidLawyerList(LegalAidLawyerDTO legalAidLawyerDTO) {
        log.info("legalAidLawyerDTO,{}",legalAidLawyerDTO);

        //用pageHelper做分页处理
//        int ps = Integer.parseInt(pageSize);//1
//        int pn = Integer.parseInt(pageNum);//5
        // SELECT la.*,su.nick_name,su.phonenumber,su.sex,su.email,su.avatar
        //        from legal_aid_lawyer la LEFT JOIN sys_user su ON la.user_id = su.user_id  limit 0,10
        //开始分页查询
       int pageSize = Integer.parseInt(legalAidLawyerDTO.getPageSize());
       int pageNum = Integer.parseInt(legalAidLawyerDTO.getPageNum());
        PageHelper.startPage(pageNum,pageSize);

        Page<LegalAidLawyerVO> lawyerVOPage = legalAidLawyerWxMapper.selectLegalAidLawyerList(legalAidLawyerDTO);

        List<LegalAidLawyerVO> volist = lawyerVOPage.getResult();
        List<LegalAidLawyerVO> list = volist.stream().map(item -> {
            if ("1".equals(item.getSex())) {
                item.setSex("男");
            } else if ("0".equals(item.getSex())) {
                item.setSex("女");
            }
            if ("yes".equals(item.getDhStatue())) {
                item.setDhStatue("在线");
            } else {
                item.setDhStatue("离线");
            }
            if ("yes".equals(item.getImageStatue())) {
                item.setImageStatue("在线");
            } else {
                item.setImageStatue("离线");
            }
            if ("yes".equals(item.getOnlineStatue())) {
                item.setOnlineStatue("在线");
            } else {
                item.setOnlineStatue("离线");
            }
            return item;
        }).collect(Collectors.toList());


        return lawyerVOPage;
    }

    /**
     * 根据id查询律师信息和用户公共信息
     * @param id
     * @return
     */
    public LegalAidLawyerVO selectLawyerWithUserById(Long id) {
        //1.查询律师信息
        LegalAidLawyer legalAidLawyer = legalAidLawyerWxMapper.selectLegalAidLawyerById(id);
        Long userId = legalAidLawyer.getUserId();
        //2.属性拷贝进VO
        LegalAidLawyerVO legalAidLawyerVO = new LegalAidLawyerVO();
        BeanUtil.copyProperties(legalAidLawyer,legalAidLawyerVO);

        //3.查询用户共=公共信息
        SysUser user = sysUserWxMapper.selectUserById(userId);
        String phonenumber = user.getPhonenumber();
        //4.属性拷贝进VO
        BeanUtil.copyProperties(user,legalAidLawyerVO);


        return legalAidLawyerVO;
    }




}
